<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/23 0023
 * Time: 下午 5:32
 */

namespace App\Process;

use EasySwoole\Core\Component\Logger;
use EasySwoole\Core\Swoole\Process\AbstractProcess;
use Swoole\Process;
use App\Utility\BaseEnum;
use App\WebSocket\Controller\games\PlayHelp;
use App\Common\BaseComm;
use EasySwoole\Core\Swoole\Memory\TableManager;

/**
 * 百人牛牛异步数据推送
 * 
 * 
 * @author Administrator
 *
 */
class GameSync extends AbstractProcess
{
    protected $logFile = 'user-login';
    
    /**
     * 处理异步数据
     */
    public function run(Process $process)
    {
        BaseEnum::Logger("异步统计游戏数据：启动线程[playSyncGameStatus] SUCCESS -- (每秒检测一次) ！",$this->logFile );
        
        $this->addTick(1000 , function () {
            try {
                
                //TODO 记录日志
                $logText = '线程【' . $this->getProcessName() . '】 开始执行====>'.BaseEnum::getNowTime(true);
                BaseEnum::Logger($logText,$this->logFile );
                Logger::getInstance()->console($logText);
                
                
                $gameTable = TableManager::getInstance()->get("games");
                BaseEnum::Logger("运行游戏数据：[getTable]  =>". json_encode([$gameTable]),$this->logFile );
                
                if($gameTable){
                    BaseEnum::Logger("运行游戏数据：[getTable]  =>". $gameTable->get("10000001","data") ,$this->logFile );
                    BaseEnum::Logger("运行游戏数据：[getTable]  =>". $gameTable->get("10000001","gameNo") ,$this->logFile );
                    BaseEnum::Logger("运行游戏数据：[getTable]  =>". $gameTable->get("10000001",null) ,$this->logFile );
                }
                
                
                
                /* //检测数据；
                $gameRoomIds = [1050,1051];
                //查询是否有哪些玩家进入房间列表页面并把FD获取到；
                foreach ($gameRoomIds as $groomId){
                    //房间数据
                    $gameDetail = BaseComm::distMemoryRooms($groomId,FALSE);
                    BaseEnum::Logger("运行游戏数据：[RoomID={$groomId}] 获取游戏数据要 =>". json_encode($gameDetail),$this->logFile ); 
                    $historydata = PlayHelp::getInstance()->getResultLog($groomId,"history");
                    //统计和处理房间数据内容；
                    BaseEnum::Logger("游戏历史盈亏：[RoomID={$groomId}] 获取历史开奖数据 =>". json_encode($historydata),$this->logFile ); 
                } */
                
                
                /* $startFd = 0;
                while (true){
                    //所有FD用户
                    $fds = ServerManager::getInstance()->getServer()->connection_list($startFd, 10);
                    if ($fds===false or count($fds) == 0) {
                        echo $this->getProcessName() ." => finish\n";
                        break;
                    };
                    $startFd = end($fds);
                    Logger::getInstance() ->log ('startFd:' . $startFd, 'heartbeat');
                    $logInfo = isset($fds) && $fds ? json_encode($fds) : '';
                    $fdsCount = isset($fds) && $fds ? count($fds) : 0;
                    Logger::getInstance() -> log('total fds:' . $fdsCount . '==>' . $logInfo, 'heartbeat');

                    if (!$fds) return false;

                    foreach ($fds as $k => $fd) {
                        Logger::getInstance()->log('HeartBeat Send Data:' . $fd . '==> 检测心跳数据；发送====PONG', 'heartbeat');
                        $result = ServerManager::getInstance()->getServer()->push($fd, "PONG");
                        Logger::getInstance()->log('HeartBeat Send Result:' . $fd . '==>' . json_encode($result), 'heartbeat');
                        if ($result) continue;
                        //如果发送失败，说明链接被关闭了，清理
                        Logger::getInstance()->log('Check Current FD is Closed , FD = '.$fd, 'heartbeat');
                    }
                    Logger::getInstance()->log('---------------heartbeat-end----------------', 'heartbeat');
                } */
                
                
                
                return true;
            } catch (\Throwable $e) {
                BaseEnum::Logger("异步操作出现异常：[线程--playSyncGameStatus]  错误 =>". $e->getMessage(),$this->logFile ); 
                return false;
            }
        });
    }

    public function onShutDown()
    {
        // TODO: Implement onShutDown() method.
    }

    public function onReceive(string $str, ...$args)
    {
        // TODO: Implement onReceive() method.
    }
}